package com.sgzn.sgpay.service.tcp.impl;

import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.service.tcp.SgTcpServer;
import com.sgzn.sgpay.service.tcp.api.TcpResetTerminalService;
import com.sgzn.sgpay.service.tcp.model.terminal.request.TcpResetTerminalRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.Socket;

/**
 * Created by linliangfang 13580393408
 * DATETIME:2018/7/14 16:49
 * Description:
 */
@Service
@Slf4j
public class TcpResetTerminalServiceImpl implements TcpResetTerminalService {

    /**
     * 发起复位终端请求
     * @param request
     */
    @Override
    public SgsResultResponse<String> tcpResetTerminalRequest(TcpResetTerminalRequest request){
        Socket socket =  SgTcpServer.socketList.get(request.getDeviceId());
        if (socket == null){
            log.info("设备 {} 连接异常，请检查网络",request.getDeviceId());
            return null;
        }
        String sendBuffer = request.toString() ;
        log.info("复位终端请求报文 {}" , sendBuffer );
        try {
            socket.getOutputStream().write(sendBuffer.getBytes("UTF-8"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return SgsResponseUtil.INSTANCE.success("发送复位终端请求成功");
    }
}
